﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CARMSBusiness;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using VitalShining.WebControls;

namespace CARMS.AdminTemplate
{
    [Function("Admin.ReportPropertyList")]
    public partial class ReportPropertyList : PageBase
    {
        private int CurrentReportCategoryID
        {
            get
            {
                return GetIntByParameter("ReportCategoryID");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                PageQueryAdapter1.PerformQuery();
            }

        } 

        protected void PageQueryAdapter1_PerformGetPageData(object sender, PerformGetPageDataEventArgs e)
        {
            SortExpression sort;
            switch (gridPropertyList.SortExpression)
            {
                case "Name":
                    sort = new SortExpression(ReportPropertyFields.PropertyName | (SortOperator)(int)gridPropertyList.SortDirection);
                    break;
                case "PropertyType":
                    sort = new SortExpression(ReportPropertyFields.PropertyType | (SortOperator)(int)gridPropertyList.SortDirection);
                    break;
 
                case "CreateDate":
                    sort = new SortExpression(ReportPropertyFields.CreateDate | (SortOperator)(int)gridPropertyList.SortDirection);
                    break; 
                default:
                    sort = new SortExpression(ReportPropertyFields.PropertyName | SortOperator.Ascending);
                    break;
            }
            var collection = new ReportPropertyCollection();
             collection.GetMulti((PredicateExpression)e.Condition,0, sort,null,e.PageIndex + 1, e.PageSize);
            e.DataSource = collection;
        }

        protected void PageQueryAdapter1_PerformGetRecordCount(object sender, PerformGetRecordCountEventArgs e)
        {
            var collection = new ReportPropertyCollection();
            e.RecordCount = collection.GetDbCount((PredicateExpression)e.Condition);
        }
        protected void PageQueryAdapter1_PerformGetCondition(object sender, PerformGetConditionEventArgs e)
        {
            var condition = new PredicateExpression();

            if (txtPropertyName.Text.Trim().Length > 0)
            {
                condition.Add(ReportPropertyFields.PropertyName % ("%" + txtPropertyName.Text.Trim() + "%"));
            }

            condition.Add(ReportPropertyFields.ReportCategoryId == CurrentReportCategoryID);
 
            e.Condition = condition;
        }
        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            PageQueryAdapter1.PerformQuery();
        }
        protected void btnImport_Click(object sender, EventArgs e)
        {
            //Import();
        }

        protected void gridPropertyList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var entity = (ReportPropertyEntity)e.Row.DataItem;  

            }
        }
 
        protected void gridPropertyList_OnRowCommand(object sender,GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "EditProperty":
                    RediretResolveUrl(ResolveUrl(SiteUrls.AdminTemplate.ReportPropertyDetail) + "?PropertyId=" + e.CommandArgument + "&ReportCategoryID=" + CurrentReportCategoryID);
                    break;
                case "DeleteProperty":
                    break;
            }
        }

        protected void gridList_OnRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gridPropertyList.EditIndex = -1;
            PageQueryAdapter1.PerformQuery();
        }
        protected void gridList_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            var txtSortIndex = gridPropertyList.Rows[e.RowIndex].Cells[3].FindControl("txtSortIndex") as TextBox;
            var txtColspan = gridPropertyList.Rows[e.RowIndex].Cells[3].FindControl("txtColspan") as TextBox;

            var propertyID = int.Parse(gridPropertyList.DataKeys[e.RowIndex].Value.ToString());
            var entity = new ReportPropertyEntity(propertyID);
            entity.SortIndex = int.Parse(txtSortIndex.Text.Trim());
            entity.Colspan = int.Parse(txtColspan.Text.Trim()); 
            var boolSave = entity.Save();
            if (boolSave)
            {
                gridPropertyList.EditIndex = -1;
                PageQueryAdapter1.PerformQuery();
            }
            else
            {
                base.Alert("Save failed.");
                return;
            }

        }
        protected void gridList_OnRowEditing(object sender, GridViewEditEventArgs e)
        {
            gridPropertyList.EditIndex = e.NewEditIndex;
            PageQueryAdapter1.PerformQuery();

        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            PageQueryAdapter1.PerformQuery();
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            Response.Redirect(SiteUrls.AdminTemplate.ReportPropertyDetail+"?ReportCategoryID="+CurrentReportCategoryID);
        }

    }
}
